//
// @Author: zhengqi Wu
// @Description:
// @Version: 1.0.0
// @Date: 2021/3/23 16:04
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const encoding = require('../encoding')
const {TlvComponentBase, InitialAble} = require('./TlvComponentBase')
var aggregation = require('../common/aggregation')//实现了多继承

class MustBeRefresh extends aggregation(TlvComponentBase,InitialAble,encoding.SelfEncodingBase){
    constructor() {
        super();
        this.mustBeRefresh = false
    }

    getMustBeRefresh(){
        return this.mustBeRefresh
    }

    setMustBeRefresh(mustBeRefresh){
        this.mustBeRefresh = mustBeRefresh
        this.doInitial()
    }

    /**
     * @description 将 MustBeRefresh 线速编码为一个 TLV
     * @param enc
     * @returns {number}
     */
    wireEncode(enc){
        // 这是一个存在性组件，所以如果值为false，则无需编码
        if(!this.mustBeRefresh) return 0
        let totalLength = 0
        try{
            // 编码 TLV-LENGTH
            let tmpLen = enc.prependVarNumber((totalLength))
            totalLength += tmpLen

            // 编码 TLV-TYPE
            tmpLen = enc.prependVarNumber((encoding.tlv.ComponentType.TlvMustBeRefresh))
            totalLength += tmpLen

            return totalLength
        }catch (e){
            throw e
        }

    }

    /**
     * @description 从 TLV Block 中解码出一个 MustBeRefresh
     * @param block
     */
    wireDecode(block){
        try{
            if(encoding.tlv.expectType(block.getType(), (encoding.tlv.ComponentType.TlvMustBeRefresh)))
                // 这是一个存在性组件，所以存在则为 true
                this.setMustBeRefresh(true)
        }catch (e){
            throw e
        }


    }
}

module.exports = {MustBeRefresh:MustBeRefresh}
